package com.jziver.library.service.impl;

import com.jziver.library.entiry.LibraryCard;
import com.jziver.library.entiry.User;
import com.jziver.library.repository.UserRepository;
import com.jziver.library.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Administrator on 2016/7/6.
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserRepository userRepository;

    @Override
    public User findOne(Integer id) {
        return userRepository.findOne(id);
    }

    @Override
    public List<User> findAll() {

        return userRepository.findAll();
    }

    @Override
    public void register(User user) {
        userRepository.save(user);
    }

    @Override
    public User login(User user) {
        return userRepository.login(user.getUsername(),user.getPassword());
    }

    @Override
    public void update(User user) {
        userRepository.saveAndFlush(user);
    }

    @Override
    public void addLibraryCard(User user,LibraryCard libraryCard) {
        userRepository.addLibraryCard(user.getId(),libraryCard);
    }



    @Override
    public boolean validateUsername(String usename) {
        return userRepository.findUserByUsername(usename)==null;
    }
}
